package com.erpanderp.wam.ac.model.service.project;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.project.Journey;
import com.erpanderp.wam.ac.model.entity.project.ProjectAssignment;
import com.erpanderp.wam.usm.model.entity.timesheet.TimesheetDetail;
import com.erpanderp.wam.util.HibernateUtil;

public class JourneyDAO {
	public  Journey deleteSelectedOnwardJourney(Journey journey){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.delete(journey);
		session.getTransaction().commit();
		return journey;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public  Journey deleteSelectedReturnJourney(Journey journey){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.delete(journey);
		session.getTransaction().commit();
		return journey;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
	public List<Journey> getOnwardJourneyDetailsByProjectAssignmentIdJourneyType(Long projectAssignmentId,String journeyType) {
		System.out.println("IN GETPROJECT ASSIGNMENAT........");
		System.out.println("projectAssignmentId...."+projectAssignmentId);
		System.out.println("journeyType........"+journeyType);
		List<Journey> onwardJourneyDetails = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from Journey j where j.projectAssignment.projectAssignmentId =:projectAssignmentId and j.journeyType =:journeyType";
			System.out.println("QUERY "+queryString);
			Query query = session.createQuery(queryString).setLong("projectAssignmentId", projectAssignmentId).setString("journeyType", journeyType);
			onwardJourneyDetails =query.list();
			System.out.println("ONWARD JOURNEY LIST "+onwardJourneyDetails.size());
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return onwardJourneyDetails;
	}

	public List<Journey> getReturnJourneyDetailsByProjectAssignmentIdJourneyType(Long projectAssignmentId,String journeyType) {
		System.out.println("IN GETPROJECT ASSIGNMENAT........");
		System.out.println("projectAssignmentId...."+projectAssignmentId);
		System.out.println("journeyType........"+journeyType);
		List<Journey> returnJourneyDetails = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from Journey j where j.projectAssignment.projectAssignmentId =:projectAssignmentId and j.journeyType =:journeyType";
			System.out.println("QUERY "+queryString);
			Query query = session.createQuery(queryString).setLong("projectAssignmentId", projectAssignmentId).setString("journeyType", journeyType);
			returnJourneyDetails =query.list();
			System.out.println("ONWARD JOURNEY LIST "+returnJourneyDetails.size());
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return returnJourneyDetails;
	}
	
	public Journey updateOnwardJourneyDetails(Journey onwardJourney) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(onwardJourney);
			session.getTransaction().commit();
			return onwardJourney;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public Journey updateReturnJourneyDetails(Journey returnJourney) {
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(returnJourney);
			session.getTransaction().commit();
			return returnJourney;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
